package com.shengzai

import scala.io.{BufferedSource, Source}

object Code21List {

  def main(args: Array[String]): Unit = {
    // GROUP BY 是将相同的元素汇集到同一个List中
    val list: List[Int] = List[Int](1, 2, 3, 4, 5, 7,1,2,3,4)
    val groupByList: Map[Int, List[Int]] = list.groupBy((em: Int) => {
      em
    })
    println(groupByList)

    // flatMap
    // 对数据进行扁平化处理
    // 对单词进行切分，将切分后的结果保存在一个List中

    val worlds: List[String] = List[String]("Hello World", "Hello Java", "Hello Python", "Hello Scala")
    //对单词切分
    val splitWorld: List[Array[String]] = worlds.map((word: String) => {
      word.split(" ")
    })
    //将每一个单词保存到List中
    val oneLineWords: List[String] = splitWorld.flatMap((oneLineWord: Array[String]) => {
      oneLineWord
    })
    println(oneLineWords)

    // 排序 sorted

    val numList: List[Int] = List[Int](19, 24, 24, 53, 215, 243, 24, 523, 54, 1234, 41)
    val sortedList: List[Int] = numList.sorted

    println(numList)
    println(sortedList) //倒序
    println(sortedList.reverse)//升序


    //
    val studentList: List[String] = Source.fromFile("hadoop_code/src/data/students.txt").getLines().toList
    val ResList: List[studentsInfo] = studentList.map((lines: String) => {
      val studentSplit: Array[String] = lines.split(",")
      new studentsInfo(studentSplit(0), studentSplit(1), studentSplit(2).toInt, studentSplit(3), studentSplit(4))
    })
    //倒序
    val orderByList: List[studentsInfo] = ResList.sortBy((oneStudent: studentsInfo) => {
      oneStudent.age
    })
    orderByList.foreach(println)



    val sortWithList: List[studentsInfo] = ResList.sortWith((current: studentsInfo, next: studentsInfo) => {
      // 拿前后两个元素的age进行比较，返回true则是倒序排序
      current.age > next.age
    })
    sortWithList.foreach(println)



  }

}
